﻿package
{
	import fl.video.FLVPlayback;
	import flash.display.*;
	import flash.net.*;
	import flash.events.*;
	import flash.text.*;
	import flash.geom.Rectangle;
	import flash.display.MovieClip;

	import VideoBtn;
	 
	public class Strip extends Sprite{
		private var vp:FLVPlayback = new FLVPlayback();
		private var imageLoader:Loader; //the asset imageLoader
		
		private var tf:TextField = new TextField(); 
		private var progressOutput:TextField; // the text display field 
		private var txtTrace:TextField;
		private var container:Sprite = new Sprite();
		
		private var mLoader = new MLoader();

		var vb:VideoBtn;
		
		var sr:String;
		private var videoYes:Boolean=false;
		private var videoClose:Sprite = new Sprite();
		//Movement 
		private var markerContainer:Sprite = new Sprite() ;
		private var vx:Number;
		private var vy:Number;
		private var bounce:Number = -0.1;//-0.7;
		private var gravity:Number =.5;
		private var oldX:Number;
		private var oldY:Number;
		private var friction:Number = 0.5;
		

		//constructor
		public function Strip(){
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align=StageAlign.TOP_LEFT;
			
			createProgressIndicator();//create the progress indicator
			
			var xmlUrlloader:URLLoader = new URLLoader();
            configureXMLListeners(xmlUrlloader);
			//var xmlURLRequest:URLRequest = new URLRequest("content.xml");
			
			
			var keyStr:String;
			var valueStr:String;
			var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
			//progressOutput.text="mooo";
			
			
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.border = true;
			addChild(tf);
			tf.x=-2000;

			tf.appendText("params:" + "\n"+"^^");
			try {
			   // var keyStr:String;
			  //  var valueStr:String;
			   // var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
			    //var build:String="x";
				for (keyStr in paramObj) {
			        valueStr = String(paramObj[keyStr]);
			        tf.appendText("\t" + keyStr + ":\t" + valueStr + "\n");
					
					if(keyStr=="SiteRoot"){
					   sr=valueStr;
					}
					if(keyStr=="SettingsFilePath"){
						var SettingsFilePath:String=valueStr;
						//build=valueStr;
						//tf.appendText("build1:["+build+"]");
					}
					if(keyStr=="AcmsTF"){
					   var AcmsTF:String=valueStr;
					   
					   tf.appendText("AcmsTF:["+AcmsTF+"]");
					   //build+="&"+keyStr+"="+valueStr;
					   //tf.appendText("build2:["+build+"]");
					}
					
					if (AcmsTF!=null){
						var build:String;
						build=SettingsFilePath+"&AcmsTF="+AcmsTF;
					  	tf.appendText("build:"+build);
					}else{
						var build:String;
						build=SettingsFilePath
					}
				}
			} catch (error:Error) {
				    tf.appendText(error.toString());
			}
			
			var xmlURLRequest:URLRequest = new URLRequest(build+"");
			//var xmlURLRequest:URLRequest = new URLRequest("xxhttp://localhost/Parlourwood/default.aspx?AcmsSV=Pending&AcmsTF=FlashXml");
			//var xmlURLRequest:URLRequest = new URLRequest("xxhttp://localhost/Parlourwood/default.aspx?AcmsSV=Pending&AcmsTF=FlashXml");
			xmlUrlloader.load(xmlURLRequest);
			
		}
		
		private function createProgressIndicator():void{
			progressOutput = new TextField();
			progressOutput.autoSize = TextFieldAutoSize.LEFT;
			progressOutput.border=true;
			progressOutput.background=true;
			progressOutput.selectable=false;
			progressOutput.text="Loading...";
			progressOutput.x=-2000;
		}
		
		private function xmlMLoad(urlRequest:URLRequest):void{
			//xmlLoader.load(urlRequest);
			/*if (!contains(progressOutput)){
				addChild(progressOutput);
			}*/
		}
		
		private function loadProgressListener(e:ProgressEvent):void{
			progressOutput.text = "Loading: " + Math.floor(e.bytesLoaded/1024)+ "/" + Math.floor(e.bytesTotal/1024)+"kb"
		}
		
		private function xmlInitListener(e:Event):void{
			}
		
		private function configureXMLListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.COMPLETE, xmlCompleteHandler);
            dispatcher.addEventListener(ProgressEvent.PROGRESS, xmlProgressHandler);
			dispatcher.addEventListener(IOErrorEvent.IO_ERROR, xmlIoErrorHandler);
        }

        private function xmlCompleteHandler(event:Event):void {
            var urlloaderb:URLLoader = URLLoader(event.target);
            //trace("completeHandler: " + urlloaderb.data);
			var myXML:XML =new XML(urlloaderb.data);
			//trace(myXML.IMAGE[0].@itemURL);   
			//trace(employees.employee.name);             
			
			createImageLoader();//create imageLoader object and register for events
			
			imageMLoad(new URLRequest(myXML.IMAGE[0].@itemURL));//start  the load operation
			
			for each (var item:XML in myXML.VIDEOS.VIDEO){
				videoYes=true;
			//trace(myXML.VIDEOS.VIDEO[0].@posX +"//"+ myXML.VIDEOS.VIDEO[0].@posY)
				//trace(item);
				
				var vb:VideoBtn= new VideoBtn(item.@itemURL, 0xffdd00);
				
   
				//var myObject = new Object(); //How to setup uniqu Object name?
				//vb.name = "vb"+1
				var tvx:Number=item.@posX;
				var tvy:Number=item.@posY;
				vb.x+=tvx+31;
				vb.y+=tvy+31;
				var url:String=item.@itemURL;
				//this.setACode(url)
				trace("this"+this.name)
				
				//var animate:Animate = new Animate();
				//vpHolder.addChild(animate);
				//vb.addChild(animate);
				container.addChild(vb);	
				
				//container.addChild(vpBtn);	
				trace(vb.name)
				this.addEventListener(MouseEvent.MOUSE_UP, vbMouseUp);
				
				
				//var px:Number=item.@posX ;
				//var py:Number=item.@posY ;
				//trace(px+"//"+py);
				//addButton(px, py);
				
				//var px:Number=myXML.VIDEOS.VIDEO[0].@posX ;
				//var py:Number=myXML.VIDEOS.VIDEO[0].@posY ;
			}
			//addButton(p,60);
        }
		
		
		function  vbMouseUp(event:MouseEvent):void{
				//tf.text=("getACode()before:"+event.target.name);
				var b:String=event.target.getACode();
				
				//trace("b:"+b);
				vp.source= b;
				
				vp.visible=true;
				vp.play();
				//trace(event.target.x)
		}
		
        private function xmlProgressHandler(event:ProgressEvent):void {
          //  trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
        }
		
		private function xmlIoErrorHandler(event:IOErrorEvent):void {
            trace("ioErrorHandler: " + event);
        }
		
		private function createImageLoader():void{
			imageLoader = new Loader();//create the imageLoader
			imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,loadProgressListener);
			imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,imageCompleteListener);
			imageLoader.contentLoaderInfo.addEventListener(Event.INIT,imageInitListener);
			imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,imageIoErrorListener);
		}
		
		
		
		private function imageMLoad(urlRequest:URLRequest):void{
			imageLoader.load(urlRequest);
			if (!contains(progressOutput)){
				addChild(mLoader);
				addChild(progressOutput);
				mLoader.y=(stage.stageHeight/2)-(mLoader.height/2);
				mLoader.x=(stage.stageWidth/2)-(mLoader.width/2);
			}
		}
		
		private function imageInitListener(e:Event):void{
			txtTrace = new TextField();
			txtTrace.autoSize = TextFieldAutoSize.LEFT;
			txtTrace.border=true;
			txtTrace.background=true;
			txtTrace.selectable=false;
			txtTrace.x=-2000;
			txtTrace.text=".";
			
			var markerShape:Shape = new Shape();
			markerShape.graphics.lineStyle(0,0xe73e97);
			markerShape.graphics.beginFill(0xe73e97,1)
			markerShape.graphics.drawRect(0,0,26,4);
			
			markerContainer.addChild(markerShape);
			
			addChildAt(container,0);
			
			addChild(txtTrace);
			
			container.addChildAt(imageLoader.content, 0);//add loaded asset to display list
			
			var markerLine:Shape = new Shape();
			markerLine.graphics.lineStyle(0, 0x343536);
			markerLine.graphics.beginFill(0xffffff,1)
			markerLine.graphics.drawRect(0,2,980,0);
			addChild(markerLine);
			addChild(markerContainer);
		}
		
		//invoked when the asset has been fully loade
		private function imageCompleteListener(e:Event):void{
			tf.appendText("imageCompleteListener");
			//tf.text="imageCompleteListener";
			removeChild(progressOutput);
			removeChild(mLoader);
			nextStep();
		}
		
		//listner invoked when a load error occurs
		private function imageIoErrorListener(e:IOErrorEvent):void{
			progressOutput.text="Image Load Error";//+ e.text;
		}
		
		private function nextStep():void{
			tf.appendText("nextStep");
			//tf.text="nextStep";
			
			
			container.addEventListener(MouseEvent.MOUSE_DOWN, mOnMouseDown);
			addEventListener(Event.ENTER_FRAME, mOnEnterFrame);
			
			if(videoYes==true){
				addVideo();
			}
		}
		
		private function addVideo():void{
			tf.appendText("add video");
			//tf.text="addVideo";
			vp.width=480;
			vp.height=270;
			vp.y=(stage.stageHeight/2)-(vp.height/2);
			vp.x=(stage.stageWidth/2)-(vp.width/2);
			vp.skin=sr+"/flash/SkinOverPlaySeekMute.swf";
			vp.skinBackgroundColor=0x444547;
			vp.skinAutoHide=true;
			
			vp.source="http://www.helpexamples.com/flash/video/cuepoints.flv";
			//vp.source= sr+"/Flash/pwflv.flv";
			//vp.source= "http://www.armfelt.co.uk/Dev/Parlourwood/Flash/pwflv.flv";
			//vp.source= "http://localhost/Parlourwood/flash/tanquerayPW_Reel.flv";
			//vp.source= "http://localhost/Parlourwood/AcmsFile/28/PureTVC30PWReel.flv?AcmsSV=Pending";
			vp.autoPlay=false;
			vp.visible=false;
			addChild(vp)
			tf.appendText("here");
			//tf.text="here";
		}
		
		
		//////
		private function mOnEnterFrame(event:Event):void{
			//var position:Number= ((((container.x*-1)/container.width)*stage.stageWidth)*2);
			//tf.appendText("x:"+container.x+"w"+container.width+"..."+(container.x*-1)/container.width);
			//tf.text="x:"+container.x+"w"+container.width+"..."+(container.x*-1)/container.width;
			//var position:Number= ((((container.x*-1)/container.width)*stage.stageWidth));
			var position:Number=(stage.stageWidth-markerContainer.width)   /  ((container.width-stage.stageWidth)/(container.x*-1)) //((((container.x*-1)/container.width)*stage.stageWidth));
			markerContainer.x=position;
			
			//vy += gravity;
			//vy=vy*0.9;
			vx=vx*0.9;
			container.x += vx;
			container.y += vy;
			
			var left:Number = 0;
			var right:Number = stage.stageWidth;
			var top:Number = 0;
			var bottom:Number = stage.stageHeight;
			
			if(container.x> left){//if(container.x + container.width > right){
				txtTrace.text="A";
				container.x = left;//container.x = right - container.width;
				vx =bounce*vx;
			}
			
			else if(container.x <  (container.width-stage.stageWidth)*-1){//else if(container.x - container.width < left){
					txtTrace.text="B";
				container.x = (container.width - stage.stageWidth)*-1;// (container.x- container.width) ;//container.x = left + container.width;
				vx *= bounce;
			}
			
			if(container.y  < 0){
				txtTrace.text="C";
				//container.y = -10;
				//vy *= bounce;
				vy=((container.y-0)*-0.6);
			}
			else if(container.y > 0){
				txtTrace.text="D";
				/*container.y = 0;
				vy *= bounce;*/
				vy=((container.y-0)*-0.6);
			}
		}
		
		private function mOnMouseDown(event:MouseEvent):void{
			
			vp.stop();
			vp.visible=false;	
			oldX = container.x;
			oldY = container.y;
			
			stage.addEventListener(MouseEvent.MOUSE_UP, mOnMouseUp);
//			e((container.width-stage.stageWidth*-1) ,0,container.width-stage.stageWidth,0);
			var vx:Number = (container.width - stage.stageWidth)*-1;
			var vy:Number =0 ;
			var vw:Number = container.width - stage.stageWidth;
			var vh:Number = 0;
			//tf.appendText(":"+","+vx+","+vy+","+vw+","+vh);
			//tf.text=":"+","+vx+","+vy+","+vw+","+vh;
			var bounds:Rectangle = new Rectangle(vx,vy,vw,vh);
			container.startDrag(false, bounds);
			removeEventListener(Event.ENTER_FRAME, mOnEnterFrame);
			addEventListener(Event.ENTER_FRAME, trackVelocity);
		}
		
		private function mOnMouseUp(event:MouseEvent):void{
			//tf.text="mOnMouseUp";
			stage.removeEventListener(MouseEvent.MOUSE_UP, mOnMouseUp);
			container.stopDrag();
			removeEventListener(Event.ENTER_FRAME, trackVelocity);
			addEventListener(Event.ENTER_FRAME, mOnEnterFrame);
		}
		
		private function trackVelocity(event:Event):void{
			vx = (container.x - oldX);
			vy = (container.y - oldY);
			oldX = container.x;
			oldY = container.y;
		}
		//////
		
		private function addButton(px,py):void{
			var vButton:Shape = new Shape();
			vButton.graphics.lineStyle(1, 0xddffff, 0.0);
			vButton.graphics.beginFill(0xddffff,0.5);
			vButton.graphics.drawCircle(0,0,40);
			vButton.x=px+40;
		 	vButton.y=py+40;
			container.addChild(vButton);
			//this.vButton.addEventListener(MouseEvent.MOUSE_UP, vButtonEvent);
		}
		
		public function vButtonEvent(event:MouseEvent):void{
			//Logger.info(event.type);
			//trace("event"+event.type)
		}
	}
}
